/* Files include */
#include "platform.h"
#include "app_drive.h"
#include "main.h"
#include "HBS1621.h"
#include "app_process.h"
#include "CMT2300A_hal.h"
#include <string.h>

#include "app_process.h"
#include "mm32_device.h"
#include "cmt2300a.h"
#include "hal_conf.h"
#include "radio.h"
#include "mm32_port.h"
#include <stdio.h>
#include "hal_uart.h"
#include "pt.h"    

char g_rx2_buff[100];
uint8_t g_rx2_counter;
uint8_t at_state=0;
uint16_t at_process=0;
extern sys_status_t sys_status;


#define POWER_ON_4G() GPIO_ResetBits(GPIOA, GPIO_Pin_11)
#define POWER_OFF_4G() GPIO_SetBits(GPIOA, GPIO_Pin_11)
#define PWKEY_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_12)
#define PWKEY_HIGHT() GPIO_SetBits(GPIOA, GPIO_Pin_12)

#define EN_5V_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_5)
#define EN_5V_HIGHT() GPIO_SetBits(GPIOB, GPIO_Pin_5)

#define YUYIN_LOW() GPIO_ResetBits(GPIOB, GPIO_Pin_6)
#define YUYIN_HIGHT() GPIO_SetBits(GPIOB, GPIO_Pin_6)



void init_4G()
{
	GPIO_InitTypeDef GPIO_InitStruct;

    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_11 | GPIO_Pin_12;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_StructInit(&GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_5 | GPIO_Pin_6;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStruct);

    POWER_ON_4G();
    PWKEY_LOW();
    PLATFORM_DelayMS(1000);

    EN_5V_HIGHT();
    YUYIN_HIGHT();

    {

    	GPIO_InitTypeDef GPIO_InitStruct;
	    NVIC_InitTypeDef NVIC_InitStruct;
	    UART_InitTypeDef UART_InitStruct;

	    RCC_APB1PeriphClockCmd(RCC_APB1ENR_UART2, ENABLE);

	    UART_StructInit(&UART_InitStruct);
	    UART_InitStruct.BaudRate      = 115200;
	    UART_InitStruct.WordLength    = UART_WordLength_8b;
	    UART_InitStruct.StopBits      = UART_StopBits_1;
	    UART_InitStruct.Parity        = UART_Parity_No;
	    UART_InitStruct.HWFlowControl = UART_HWFlowControl_None;
	    UART_InitStruct.Mode          = UART_Mode_Rx | UART_Mode_Tx;
	    UART_Init(UART2, &UART_InitStruct);

	    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);

	    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
	    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

	    GPIO_StructInit(&GPIO_InitStruct);
	    GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_2;
	    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	    GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
	    GPIO_Init(GPIOA, &GPIO_InitStruct);

	    GPIO_StructInit(&GPIO_InitStruct);
	    GPIO_InitStruct.GPIO_Pin  = GPIO_Pin_3;
	    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;
	    GPIO_Init(GPIOA, &GPIO_InitStruct);

	    NVIC_InitStruct.NVIC_IRQChannel = UART2_IRQn;
	    NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
	    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	    NVIC_Init(&NVIC_InitStruct);

	    UART_Cmd(UART2, ENABLE);

	    UART_ITConfig(UART2, UART_IT_RXIEN, ENABLE);

	    printf("4G init ok!\r\n");

    }
}


void UART2_Send_Str(char* dat)
{
	int i=0;
	int len=strlen(dat);
	while(len>i)
	{
		UART_SendData(UART2, dat[i++]);
		while (RESET == UART_GetFlagStatus(UART2, UART_FLAG_TXEPT)){}
	}
	
}

enum AT_STATE
{
	AT_TEST,
	AT_TEST_WAIT,
	AT_OK,
	AT_NET_OK,
	AT_SERVER_OK
};

void send_text_msg_to_server(char* d)
{
	char bf2[20];
	sprintf(bf2,"AT+QISEND=0,%d\r\n",strlen(d));
	UART2_Send_Str(bf2);
	UART2_Send_Str(d);
}

int at_4G_process(struct pt *pt)
{
	#define PT_DELAY(T) time_delay=T;PT_WAIT_UNTIL(pt, --time_delay == 0);	
	#define AT_RXBUFF_CLEAR() g_rx2_counter=0;memset(g_rx2_buff,0,100);
	#define AT_SEND(X) UART2_Send_Str(X);
	static uint32_t times=10;
	static uint32_t time_delay=0;
	static uint32_t time_out=0;
	static char bf1[150];
	static char bf2[20];

	PT_BEGIN(pt);

  	while(1) {

  		AT_SEND("AT\r\n");   		
    	PT_DELAY(500);
    	printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);		
    	if(strstr(g_rx2_buff,"OK"))
    	{
    		printf("AT_OK\r\n");
    		AT_RXBUFF_CLEAR();

    		printf("open GPS!\r\n");
    		AT_SEND("AT+QGPS=1\r\n");
    		PT_DELAY(300);
    		AT_SEND("AT+QGPSCFG=\"gnssconfig\",5\r\n");    		
    		PT_DELAY(300);

    		printf("init LBS \r\n");    		
    		AT_SEND("AT+QLBSCFG=\"asynch\",1\r\n"); 
    		PT_DELAY(300);
    		AT_SEND("AT+QLBSCFG=\"token\"\r\n"); 
    		PT_DELAY(300);    
    		printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);		
    		if(!strstr(g_rx2_buff,"***"))
    		{
    			printf("set LBS token\r\n");
    			AT_SEND("AT+QLBSCFG=\"token\",\"Ip10J7wr779o6c46\"\r\n"); 		
    			PT_DELAY(300); 
    		}
    		
    		AT_RXBUFF_CLEAR();

			//检测网络
    		AT_SEND("AT+QIACT=1\r\n");
    		times=15;
    		while(--times>0)
    		{
    			if(strstr(g_rx2_buff,"OK"))
    			{
    				printf("4G NET OK! \r\n");
    				break;
    			}
    			PT_DELAY(1000);
    		}
    		printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);
    		if(!strstr(g_rx2_buff,"OK"))
			{
				printf("4G NET ERROR!\r\n");
				EN_5V_LOW();
				PT_DELAY(3000);
				EN_5V_HIGHT();
				continue;
			}    		
			printf("4G connected!\r\n");
			AT_RXBUFF_CLEAR();

			AT_SEND("AT+QICSGP=1\r\n");
			PT_DELAY(300);
			AT_RXBUFF_CLEAR();
			printf("TCP CONNECTING!\r\n");
			AT_SEND("AT+QIOPEN=1,0,\"TCP\",\"47.97.196.75\",6000,0,1\r\n");
			times=10;
    		while(--times>0)
    		{
    			if(strstr(g_rx2_buff,"+QIOPEN:"))
    			{
    				printf("TCP CONNECT OK! \r\n");
    				break;
    			}
    			PT_DELAY(1000);
    		}  
    		printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);
    		if(!strstr(g_rx2_buff,"+QIOPEN:"))
			{
				printf("TCP connecte fail!\r\n");
				EN_5V_LOW();
				PT_DELAY(3000);
				EN_5V_HIGHT();
				continue;
			}   
		
			AT_RXBUFF_CLEAR();
			printf("TCP OVER!\r\n");

			AT_SEND("AT+QISEND=0,38\r\n");
			PT_DELAY(100);
			AT_SEND("{\"opcode\":\"login\",\"did\":\"H000000001\"}\n");

			times=15;
    		while(1)
    		{
    			// printf("TCP OVER!\r\n");
    			PT_DELAY(100);
    			if(g_rx2_counter)
    			{
    				PT_DELAY(300);
    				printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);

    				if(strstr(g_rx2_buff,"+QLBS: 0,"))
					{		
						
						char* d1=strstr(g_rx2_buff,"+QLBS: 0,");		 
						char* d2=strstr(d1+9,",");
						char* d3=strstr(d2+1,"\r");

						char* longitude=d1+9;
						char* latitude=d2+1;
						d2[0]=0;
						d3[0]=0;

						sprintf(bf1,
							"{\"opcode\":\"up_location\",\"did\":\"H000000001\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"mode\":\"lbs\"}\n",
							longitude,latitude);
						sprintf(bf2,"AT+QISEND=0,%d\r\n",strlen(bf1));

						AT_RXBUFF_CLEAR();
						AT_SEND(bf2);						
						PT_DELAY(100);
						AT_SEND(bf1);
						PT_DELAY(300);
						printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);

						time_out=0;
						
					}
					else if(strstr(g_rx2_buff,"+QGPSLOC:"))
					{		
						
						char* d1=strstr(g_rx2_buff,"+QGPSLOC:");		 
						char* d2=strstr(d1+20,",");
						char* d3=strstr(d2+1,",");

						char* latitude=d1+20;
						char* longitude=d2+1;
						d2[0]=0;
						d3[0]=0;

						sprintf(bf1,
							"{\"opcode\":\"up_location\",\"did\":\"H000000001\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"mode\":\"gnss\"}\n",
							longitude,latitude);
						sprintf(bf2,"AT+QISEND=0,%d\r\n",strlen(bf1));

						AT_RXBUFF_CLEAR();
						AT_SEND(bf2);						
						PT_DELAY(100);
						AT_SEND(bf1);
						PT_DELAY(300);
						printf("g_t:%d,%s\r\n",g_rx2_counter,g_rx2_buff);

						time_out=0;
						
					}
					else if(strstr(g_rx2_buff,"come_record"))
					{
						time_out=0;
						AT_SEND("AT+QHTTPCFG=\"requestheader\",0\r\n");
						PT_DELAY(300);
						AT_SEND("AT+QHTTPURL=58,2\r\n");
						PT_DELAY(300);
						AT_SEND("http://47.97.196.75:6080/?did=H000000001&opcode=get_record");
						PT_DELAY(300);
						AT_SEND("AT+QHTTPGET=20\r\n");
						printf("{7}\r\n");
						
					}
					else if(strstr(g_rx2_buff,"+QHTTPGET: 0,200"))
					{
						time_out=0;
						AT_SEND("AT+QHTTPREADFILE=\"UFS:record.mp3\",30\r\n");
						PT_DELAY(1000);
						AT_SEND("AT+QAUDPLAY=\"record.mp3\",0\r\n");
						printf("{8}\r\n");
					}
					else if(strstr(g_rx2_buff,"post_cmd"))
					{
						char* d1=strstr(g_rx2_buff,"voice");
						char* d2=strstr(g_rx2_buff,"shock");
						char* d3=strstr(g_rx2_buff,"electric");
						if(d1)
						{
							sys_status.voice=1;
							sys_status.voice_grade=d1[8]-'0';
						}
						else if(d2)
						{
							sys_status.shock=1;
							sys_status.shock_grade=d2[8]-'0';
						}
						else if(d3)
						{
							sys_status.electric=1;
							sys_status.electric_grade=d3[11]-'0';
						}
						time_out=0;
					}
					else if(strstr(g_rx2_buff,"+CME ERROR: 9"))
					{
						printf("QAUDPLAY ERR \r\n");
						AT_SEND("AT+QAUDSTOP\r\n");
						PT_DELAY(300);
						AT_SEND("AT+QAUDPLAY=\"record.mp3\",0\r\n");
					}
					if(strstr(g_rx2_buff,"pong") || strstr(g_rx2_buff,"\"err_code\":0"))
					{
						time_out=0;
					}



    				AT_RXBUFF_CLEAR();
    			}

    			if(times++==600)
    			{
    				
					AT_SEND("AT+QGPSLOC=2\r\n");				
    			}
    			else if(times==700)
    			{
    				AT_SEND("AT+QLBS\r\n");
    				times=0;
    			}
    			else if(times>800)
    			{    				
    				AT_SEND("AT+QISEND=0,37\r\n");
					PT_DELAY(100);
    				AT_SEND("{\"opcode\":\"ping\",\"did\":\"H000000001\"}\n");
    				times=0;
    			}
    			if(time_out++>2500)
    			{
    				time_out=0;
    				printf("4G time out\r\n");
    				EN_5V_LOW();
					PT_DELAY(1000);
					EN_5V_HIGHT();
    				break;

    			}
    		}		
    		
    		
    	}
    	else
    	{
    		printf("AT_ERROR!\r\n");
    	}
    	
    	PT_DELAY(3000);
    	AT_RXBUFF_CLEAR();
   
	}
	PT_END(pt);
}
// void at_4G_process(void)
// {

	// static uint32_t at_time_counter=0;

	
	// printf("{0}\r\n");
	// TASK_DELAY(1,5000);

	// printf("{5}\r\n");	
	// switch(at_state)
	// {
	// 	case AT_TEST:		
	// 		UART2_Send_Str("AT\r\n");
	// 		at_state=AT_TEST_WAIT;
	// 		at_time_counter=0;
	// 		break;
	// 	case AT_TEST_WAIT:		
	// 		UART2_Send_Str("AT\r\n");
	// 		at_state=AT_TEST_WAIT;
	// 		break;
	// 	default:
	// 		break;	
	// }

	// if(at_time_counter++>100)
	// {
	// 	at_time_counter=0;
		
	// 	if(at_state==AT_INIT)
	// 	{
	// 		if(at_process==0)
	// 		{
	// 			UART2_Send_Str("AT\r\n");	
	// 			printf("{0}\r\n");			
	// 		}
	// 		if(at_process++>5)
	// 		{
	// 			at_process=0;
	// 		}
			
	// 	}
	// 	else if(at_state==AT_OK)
	// 	{
	// 		if(at_process==0)
	// 		{
	// 			UART2_Send_Str("AT+QIACT=1\r\n");
	// 			PLATFORM_DelayMS(100);
	// 			UART2_Send_Str("AT+QICSGP=1\r\n");
	// 			printf("{1}\r\n");
	// 		}

	// 		if(at_process++>15)
	// 		{
	// 			at_process=0;
	// 		}
	// 	}
	// 	else if(at_state==AT_NET_OK)
	// 	{
	// 		if(at_process==0)
	// 		{
	// 			UART2_Send_Str("AT+QICSGP=1\r\n");
	// 			PLATFORM_DelayMS(100);
	// 			UART2_Send_Str("AT+QIOPEN=1,0,\"TCP\",\"47.97.196.75\",6000,0,1\r\n");
	// 			printf("{2}\r\n");
	// 		}

	// 		if(at_process++>15)
	// 		{
	// 			at_process=0;
	// 		}
	// 	}
	// 	else if(at_state==AT_SERVER_OK)
	// 	{
	// 		if(at_process%15==0)
	// 		{
				
	// 		}
	// 		if(at_process>60)
	// 		{
	// 			at_process=0;
	// 			//UART2_Send_Str("AT+QLBS\r\n");
	// 		}

	// 		at_process++;
	// 	}
		
	// }
// }
// void at_analysis()
// {
// 	static uint8_t t1=0;

	
// 	if(g_rx2_counter>2)
// 	{
// 		while(!(g_rx2_buff[g_rx2_counter-2]==0x0D && g_rx2_buff[g_rx2_counter-1]==0x0A))
// 		{
// 			PLATFORM_DelayMS(300);
// 		}
// 		printf("[%s]",g_rx2_buff);	
// 		if(strstr(g_rx2_buff,"OK\r"))
// 		{
// 			printf("{3}\r\n");
// 			if(at_state==AT_INIT)
// 			{
// 				at_state=AT_OK;	
// 				at_process=0;
// 			}
// 			else if(at_state==AT_OK)
// 			{
// 				at_state=AT_NET_OK;
// 				at_process=0;
// 			}
// 		}
// 		else if(strstr(g_rx2_buff,"ERROR\r"))
// 		{
// 			at_process=0;
// 			printf("{4}\r\n");
// 		}

// 		if(strstr(g_rx2_buff,"+QIOPEN:"))
// 		{
// 			at_state=AT_SERVER_OK;
// 			at_process=0;
// 			UART2_Send_Str("AT+QISEND=0,38\r\n");
// 			PLATFORM_DelayMS(100);
// 			UART2_Send_Str("{\"opcode\":\"login\",\"did\":\"H000000001\"}\n");
// 			printf("{5}\r\n");
// 		}
// 		else if(strstr(g_rx2_buff,"+QLBS:"))
// 		{
// 			if(g_rx2_counter>29)
// 			{
// 				char bf1[150];
// 				char bf2[20];
// 				char* d1=strstr(g_rx2_buff,",");		 
// 				char* d2=strstr(d1+1,",");
// 				char* d3=strstr(d2+1,"\r");

// 				char* longitude=d1+1;
// 				char* latitude=d2+1;
// 				d2[0]=0;
// 				d3[0]=0;

// 				sprintf(bf1,
// 					"{\"opcode\":\"up_location\",\"did\":\"H000000001\",\"longitude\":\"%s\",\"latitude\":\"%s\",\"mode\":\"lbs\"}\n",
// 					longitude,latitude);
// 				sprintf(bf2,"AT+QISEND=0,%d\r\n",strlen(bf1));
// 				UART2_Send_Str(bf2);
// 				PLATFORM_DelayMS(100);
// 				UART2_Send_Str(bf1);

// 				printf("{6}\r\n");
// 			}
// 		}
// 		else if(strstr(g_rx2_buff,"come_record"))
// 		{
// 			UART2_Send_Str("AT+QHTTPCFG=\"requestheader\",0\r\n");
// 			PLATFORM_DelayMS(100);
// 			UART2_Send_Str("AT+QHTTPURL=58,2\r\n");
// 			PLATFORM_DelayMS(100);
// 			UART2_Send_Str("http://47.97.196.75:6080/?did=H000000001&opcode=get_record");
// 			PLATFORM_DelayMS(100);
// 			UART2_Send_Str("AT+QHTTPGET=20\r\n");
// 			printf("{7}\r\n");
			
// 		}
// 		else if(strstr(g_rx2_buff,"+QHTTPGET: 0,200"))
// 		{
// 			UART2_Send_Str("AT+QHTTPREADFILE=\"UFS:record.mp3\",30\r\n");
// 			PLATFORM_DelayMS(1000);
// 			UART2_Send_Str("AT+QAUDPLAY=\"record.mp3\",0\r\n");
// 			printf("{8}\r\n");
// 		}
		
// 		g_rx2_counter=0;
// 		memset(g_rx2_buff,0,200);
// 	}

// }

